package com.example.carbon.server.info;

import com.alibaba.fastjson2.JSON;
import com.example.carbon.dao.DeviceDataDao;
import com.example.carbon.pojo.daoo.DeviceSearch;
import com.example.carbon.pojo.dto.DeviceInfoSearch;
import com.example.carbon.pojo.dto.DeviceInfoSetData;
import com.example.carbon.pojo.dto.PageLimit;
import com.example.carbon.pojo.vo.DeviceShowData;
import com.example.carbon.pojo.vo.TableData;
import com.example.carbon.server.DeviceServerInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DeviceServer implements DeviceServerInfo {
    @Resource
    DeviceDataDao deviceDataDao;
    /**
     * 更新数据
     *
     * @param data
     * @return
     */
    @Override
    public boolean updateDeviceInfo(DeviceInfoSetData data) {
        try {
            return deviceDataDao.updateInfo(data);
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 修改数据的时候，将会调用一次，来获取该修改的数据
     *
     * @param id
     * @return
     */
    @Override
    public String getDeviceSettingInfoByID(int id) {
        try {
            return JSON.toJSONString(deviceDataDao.selectDeviceInfoSettingById(id));
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteDeviceInfo(int id) {
        try {
            return deviceDataDao.deleteInfoById(id);
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 获取数据信息
     * @param deviceInfoSearch
     * @param pageLimit
     * @return
     */
    @Override
    public String searchInfo(DeviceInfoSearch deviceInfoSearch, PageLimit pageLimit) {
        try {
            DeviceSearch deviceSearch = new DeviceSearch(deviceInfoSearch, pageLimit);
            List<DeviceShowData> deviceShowData = deviceDataDao.selectDeviceInfoForSearch(deviceSearch,pageLimit);
            TableData tableData = new TableData<>(deviceShowData,
                    deviceDataDao.selectCountOfSearch(deviceSearch));
            return JSON.toJSONString(tableData);
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }
    }

    /**
     * 插入新纪录
     * @param data
     * @return
     */
    @Override
    public boolean insertNewInfo(DeviceInfoSetData data) {
        try {
            if (deviceDataDao.insertDeviceInfo(data)) {
                return true;
            }
            return false;
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }

    }

}
